package cn.yikaojiuguo.student.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.support.utils.LogUtil;

/**
 * Created by cfan_yang on 2016/1/27.
 */
public class ClickableSeekBar extends LinearLayout{

    private int mProgressColor;
    private int mBackColor;
    private int mSlider_src;
    private int mProgressHeight;
    private LinearLayout mProgress;
    private LinearLayout mBackground;
    private ImageView mSlider;
    private int mViewWidth;
    private boolean isSliding = false;
    private int mDownX;
    private OnSlideListener mOnSlideListener;

    public ClickableSeekBar(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public ClickableSeekBar(Context context) {
        this(context, null, 0);
    }

    public ClickableSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ClickableSeekBar);
        mProgressColor = typedArray.getColor(R.styleable.ClickableSeekBar_progress_color, Color.BLUE);
        mBackColor = typedArray.getColor(R.styleable.ClickableSeekBar_back_color, Color.LTGRAY);
        mSlider_src = typedArray.getResourceId(R.styleable.ClickableSeekBar_slider_src, 0);
        mProgressHeight = typedArray.getDimensionPixelSize(R.styleable
                .ClickableSeekBar_progress_height, 0);
        initView(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        mViewWidth = getWidth();
    }

    private void initView(final Context context) {
        mViewWidth = getMeasuredWidth();
        LogUtil.showInfoLog("measure","width = " + mViewWidth);
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        mProgress = new LinearLayout(getContext());
        this.addView(mProgress);
        ViewGroup.LayoutParams progressParams = mProgress.getLayoutParams();
        progressParams.height = mProgressHeight;
        progressParams.width = 0;
        mProgress.setLayoutParams(progressParams);
        mProgress.setBackgroundColor(mProgressColor);
        mSlider = new ImageView(getContext());
        this.addView(mSlider);
        ViewGroup.LayoutParams sliderParams = mSlider.getLayoutParams();
        sliderParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        sliderParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        mSlider.setLayoutParams(sliderParams);
        mSlider.setImageResource(mSlider_src);
        mBackground = new LinearLayout(getContext());
        this.addView(mBackground);
        ViewGroup.LayoutParams backParams = mBackground.getLayoutParams();
        backParams.height = mProgressHeight;
        backParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        mBackground.setLayoutParams(backParams);
        mBackground.setBackgroundColor(mBackColor);
        mSlider.setOnTouchListener(mSliderTouchListener);
    }

    public void setOnSlideListener(OnSlideListener listener) {
        this.mOnSlideListener = listener;
    }

    private OnTouchListener mSliderTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            LogUtil.showInfoLog("ClickableSeekBar", "onTouch");
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    if(mOnSlideListener!=null) {
                        mOnSlideListener.onStartSlide(ClickableSeekBar.this);
                    }
                    LogUtil.showInfoLog("ClickableSeekBar","action down");
                    isSliding = false;
                    mDownX = (int) event.getX();
                    break;
                case MotionEvent.ACTION_MOVE:
                    LogUtil.showInfoLog("ClickableSeekBar","action move");
                    isSliding = true;
                    int newX = (int) event.getX();
                    int dx = newX-mDownX;
                    if(dx == 0) {
                        isSliding = false;
                    }
                    LogUtil.showInfoLog("ClickableSeekBar","delt x = " + dx);
                    ViewGroup.LayoutParams progressParams = mProgress.getLayoutParams();
                    int newProgressWidth = progressParams.width + dx;
                    if(newProgressWidth>mViewWidth - mSlider.getWidth()) {
                        newProgressWidth = mViewWidth - mSlider.getWidth();
                    } else if(newProgressWidth<0) {
                        newProgressWidth = 0;
                    }
                    progressParams.width = newProgressWidth;
                    mProgress.setLayoutParams(progressParams);
                    LogUtil.showInfoLog("ClickableSeekBar","progress width = " + progressParams.width);
                    break;
                case MotionEvent.ACTION_UP:
                    if(mOnSlideListener!=null) {
                        mOnSlideListener.onStopSlide(ClickableSeekBar.this);
                    }
                    LogUtil.showInfoLog("ClickableSeekBar","action up");
                    break;
                case MotionEvent.ACTION_CANCEL:
                    if(mOnSlideListener!=null) {
                        mOnSlideListener.onStopSlide(ClickableSeekBar.this);
                    }
                    LogUtil.showInfoLog("ClickableSeekBar","action cancel");
                    break;
            }
            return isSliding;
        }
    };

    public float getProgress() {
        float progress = 0;
        progress = (float)mProgress.getWidth()/(mViewWidth - mSlider.getWidth());
        return progress;
    }

    public void setProgress(float progress) {
        if(mViewWidth == 0) {
            return;
        }
        ViewGroup.LayoutParams layoutParams = mProgress.getLayoutParams();
        layoutParams.width = (int) ((mViewWidth - mSlider.getWidth())*progress);
        //LogUtil.showInfoLog("setProgress","width = " + layoutParams.width);
        mProgress.setLayoutParams(layoutParams);
    }

    public void setOnSliderClickListener(OnClickListener listener) {
        mSlider.setOnClickListener(listener);
    }

    public void setSliderImgSrc(int resId) {
        mSlider.setImageResource(resId);
    }

    public interface OnSlideListener {
        public void onStartSlide(ClickableSeekBar csb);
        public void onStopSlide(ClickableSeekBar csb);
    }

}
